package me.lc.hammerheads.layer;

import me.lc.hammerheads.R;
import me.lc.hammerheads.core.GameSystem;
import me.lc.hammerheads.core.StageManager;
import me.lc.hammerheads.util.AudioHelper;

import com.wiyun.engine.actions.Action;
import com.wiyun.engine.actions.IntervalAction;
import com.wiyun.engine.actions.MoveTo;
import com.wiyun.engine.actions.RepeatForever;
import com.wiyun.engine.actions.ScaleTo;
import com.wiyun.engine.actions.Sequence;
import com.wiyun.engine.nodes.Button;
import com.wiyun.engine.nodes.Layer;
import com.wiyun.engine.nodes.Sprite;
import com.wiyun.engine.opengl.Texture2D;
import com.wiyun.engine.types.WYPoint;
import com.wiyun.engine.utils.ResolutionIndependent;

public class MainMenuBGLayer extends Layer {
	Sprite hammer;
	Sprite dwarfs;
	Button audioOnButton;
	Button audioOffButton;

	public MainMenuBGLayer() {
		Texture2D tex = GameSystem.tex_mainBg;
		Sprite sp = Sprite.make(tex);
		sp.setAnchorPercent(0, 0);
		sp.setPosition(WYPoint.make(0, 0));
		sp.setScaleX(GameSystem.Screen_Width / tex.getWidth());
		sp.setScaleY(GameSystem.Screen_ActuallyHeight / tex.getHeight());
		addChild(sp);

		hammer = Sprite.make(GameSystem.tex_mainHammer);
		hammer.setPosition(GameSystem.Screen_Width / 2.0f,
				GameSystem.Screen_ActuallyHeight * 0.758f);
		addChild(hammer);

		// hammer动画
		float startX1 = hammer.getPositionX()
				- ResolutionIndependent.resolveDp(4);
		float endX1 = hammer.getPositionX()
				+ ResolutionIndependent.resolveDp(4);
		float y1 = hammer.getPositionY();
		IntervalAction move1 = (IntervalAction) MoveTo.make(0.6f, startX1, y1,
				endX1, y1).autoRelease();
		IntervalAction back1 = (IntervalAction) move1.reverse().autoRelease();
		IntervalAction seq1 = (IntervalAction) Sequence.make(move1, back1)
				.autoRelease();
		Action repeat1 = (Action) RepeatForever.make(seq1).autoRelease();
		hammer.runAction(repeat1);

		float x2 = hammer.getPositionX();
		float startY2 = hammer.getPositionY()
				- ResolutionIndependent.resolveDp(4);
		float endY2 = hammer.getPositionY()
				+ ResolutionIndependent.resolveDp(4);
		IntervalAction move2 = (IntervalAction) MoveTo.make(0.7f, x2, startY2,
				x2, endY2).autoRelease();
		IntervalAction back2 = (IntervalAction) move2.reverse().autoRelease();
		IntervalAction seq2 = (IntervalAction) Sequence.make(move2, back2)
				.autoRelease();
		Action repeat2 = (Action) RepeatForever.make(seq2).autoRelease();
		hammer.runAction(repeat2);

		IntervalAction scale = (IntervalAction) ScaleTo.make(0.5f, 0.96f, 1.1f)
				.autoRelease();
		IntervalAction scaleback = (IntervalAction) scale.reverse()
				.autoRelease();
		IntervalAction seq3 = (IntervalAction) Sequence.make(scale, scaleback)
				.autoRelease();
		Action repeat3 = (Action) RepeatForever.make(seq3).autoRelease();
		hammer.runAction(repeat3);

		dwarfs = Sprite.make(GameSystem.tex_mainBottom);
		dwarfs.setAnchorPercent(0, 0);
		dwarfs.setPosition(WYPoint.make(0,
				-GameSystem.tex_mainBottom.getHeight()));
		dwarfs.setScaleX(GameSystem.Screen_Width
				/ GameSystem.tex_mainBottom.getWidth());
		addChild(dwarfs);

		showDwarfs();
		initAudioControl();
	}

	public void hideDwarfs() {
		// 动画
		IntervalAction dwarfsMove = (IntervalAction) MoveTo.make(0.6f,
				dwarfs.getPositionX(), dwarfs.getPositionY(),
				dwarfs.getPositionX(), -GameSystem.tex_mainBottom.getHeight())
				.autoRelease();
		dwarfs.runAction(dwarfsMove);
	}

	public void showDwarfs() {
		// 动画
		IntervalAction dwarfsMove = (IntervalAction) MoveTo.make(1.2f,
				dwarfs.getPositionX(), dwarfs.getPositionY(),
				dwarfs.getPositionX(), 0).autoRelease();
		dwarfs.runAction(dwarfsMove);
	}

	private void initAudioControl() {

		float x = GameSystem.Screen_Width / 2.0f;
		float y = GameSystem.Screen_ActuallyHeight * 0.04375f;
		audioOnButton = Button.make(R.drawable.audio_on, R.drawable.audio_on,
				this, "onAudioOnClick");
		audioOnButton.setPosition(x, y);
		this.addChild(audioOnButton);
		audioOffButton = Button.make(R.drawable.audio_off,
				R.drawable.audio_off, this, "onAudioOffClick");
		audioOffButton.setPosition(x, y);

		this.addChild(audioOffButton);

		if (StageManager.instance.enableAudio == 1) {
			audioOffButton.setVisible(false);
			AudioHelper.playMusicMenu();
		} else {
			audioOnButton.setVisible(false);
		}
	}

	public void onAudioOnClick() {
		AudioHelper.stopMusic();
		audioOffButton.setVisible(true);
		audioOnButton.setVisible(false);
		StageManager.instance.enableAudio = 0;
		StageManager.instance.saveAudioConfig();
	}

	public void onAudioOffClick() {
		AudioHelper.playMusicMenu();
		audioOffButton.setVisible(false);
		audioOnButton.setVisible(true);
		StageManager.instance.enableAudio = 1;
		StageManager.instance.saveAudioConfig();
	}
}
